home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c
- Path: gail.ripco.com!mambuhl
- From: mambuhl@ripco.com (Martin Ambuhl)
- Subject: Re: directory functions .
- X-Nntp-Posting-Host: foley.ripco.com
- Message-ID: <DLMsxw.43u@rci.ripco.com>
- Sender: usenet@rci.ripco.com (Net News Admin)
- Organization: Ripco Internet BBS Chicago
- Date: Tue, 23 Jan 1996 11:22:43 GMT
- X-Ident-Sender: mambuhl
-
- Hussein Salem <salem@ehvvs5.nwg.dec.com>
- in <30FF91CE.3F54@ehvvs5.nwg.dec.com> wrote:
-
- >I'm trying to make a small program in C handling a directory. open it and print
- >it's content
- >:
- >The problem is it can't read it ! Why ? I 've set the mode to a+rwx to that dir
- >story !
- >These are the code and if you know what's the problem please tell me why ?
-
- 1) Since dirent.h, sys/types.h, DIR *, opendir(), readdir() are none
- part of C, this is the wrong place for this (try comp.os.unix.*).
-
- 2) If your implementation is ANSI compliant, "MYMAX" is wasted motion
- (try FILENAME_MAX)
-
- 3) If your implementation is POSIX compliant, your definition of the
- struct you use for the return from readdir is wrong and unnecessary.
-
- 4) There are several mis-parenthesized lines of code.
-
- Your original code is at EOM. A corrected version follows, but only
- because your code contains serious C errors. If it had been only a
- question of your incorrect use of unix-isms, you should have had no
- expectation of help here:
-
- #include <stdio.h>
- #include <stdlib.h> /* mha - added */
- #include <dirent.h>
- #if 0
- /* mha - removed */
- #include <sys/types.h>
- #define MYMAX 14
- #endif
-
- #if 0
- /* mha - removed incorrect direct struct */
- typedef struct {
- long ino;
- char name[MYMAX + 1];
- } Dirent;
- #endif
-
- int main(int argc, char **argv)
- { /* mha - added explicit return type */
- #if 0
- char name[FILENAME_MAX]; /* mha - was "MYMAX"; unused so removed */
- #endif
- DIR *dfd;
- char *dir;
- struct dirent *dp; /* mha - was "Dirent" */
- dir = "/usr/users/salem/test";
- if ((dfd = opendir(dir)) == NULL) { /* mha - fixed parens */
- fprintf(stderr, " can't open %s\n", dir);
- return EXIT_FAILURE; /* mha - fixed return w/o value */
- }
- printf("dir opened %s\n", dir);
- if ((dp = readdir(dfd)) == NULL) {
- fprintf(stderr, " can't read %s\n", dir);
- return EXIT_FAILURE; /* mha - fixed return w/o value */
- }
- while ((dp = readdir(dfd)) != NULL) {
- printf("name = %s\n", dp->d_name); /* mha - was "dp->name".
- * added '\n' */
- }
- closedir(dfd);
- return 0; /* mha - added explicit return */
- }
-
-
- [ == salem's orginal code == ]
- >-----------------
-
- >#include <stdio.h>
- >#include <dirent.h>
- >#include <sys/types.h>
- >#define MYMAX 14
-
- >typedef struct {
- > long ino;
- > char name[MYMAX+1];
- >} Dirent;
-
- >main(int argc,char **argv){
- >char name[MYMAX];
- >DIR *dfd;
- >char *dir;
- >Dirent *dp;
-
- >dir = "/usr/users/salem/test";
-
- >if ((dfd = opendir(dir) == NULL)) {
- > fprintf(stderr," can't open %s\n",dir);
- > return;
- >}
- >printf("dir opened %s\n",dir);
- >if ( (dp = readdir(dfd)) == NULL) {
- >fprintf(stderr," can't read %s\n",dir);return;
- >}
- >while ( (dp = readdir(dfd)) != NULL) {
- > printf("name = %s",dp->name);
- > /*if ( !strcmp(dp->name,argv[1]) || !strcmp(dp->name,"..") ) continue;
- > printf("name = %s/%s \n",dir, dp->name); */
- >}
- >closedir(dfd);
-
-
- >}
-
- Lose the 16-line .sig If 4 lines aren't enough, then your ego is too
- big.
-
-
- --
- * Martin Ambuhl net: mambuhl@ripco.com
- * Chicago, IL (USA)
-